package data

import (
	"fmt"
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
	"strconv"
)

func (m MemberRepo) UpdateAuthState(id, authType int) error {
	var sql string
	o := orm.NewOrm()
	if authType == 1 {
		sql = SQL_Mbr_Upd_Auth_State
	} else if authType == 2 {
		sql = SQL_Mbr_Upd_Carrier_Owner_Auth_State
	} else if authType == 3 {
		sql = SQL_Mbr_Upd_Enti_Cargo_Owner_Auth_State
	} else if authType == 4 {
		sql = SQL_Mbr_Upd_Personal_Cargo_Owner_Auth_State
	} else {
		sql = SQL_Mbr_Upd_Emp_Auth_State
	}

	_, err := o.Raw(sql, id).Exec()
	return err
}

func (m MemberRepo) UpdateUserNameAndIDCardNo(memberId int, name, idCardNo string) error {
	o := orm.NewOrm()
	_, err := o.Raw(SQL_Mbr_Upd_Name_And_IDCardNo, name, name, idCardNo, memberId).Exec()
	return err
}

func (m MemberRepo) GetMemberType(id int) (string, error) {
	var list orm.ParamsList
	o := orm.NewOrm()
	num, err := o.Raw(SQL_Mbr_Get_Member_Type, id).ValuesFlat(&list)
	if num == 1 {
		return list[0].(string), nil
	}
	return "0", err
}

func (m MemberRepo) GetAuthState(id int) (string, string, string, string, error) {
	var num int64
	var err error
	var list orm.ParamsList
	var mbrType, pAuthState, eAuthState, stateReason string
	o := orm.NewOrm()
	num, err = o.Raw(SQL_Mbr_Get_Personal_Auth_State, id).ValuesFlat(&list)
	if num >= 0 && err == nil {
		pAuthState = list[0].(string)
		mbrType = list[1].(string)
		if list[2] != nil {
			stateReason = list[2].(string)
		}
		if mbrType != Mbr_no_role_type {
			eAuthState, err = m.GetEntiAuthState(id, mbrType)
		} else {
			eAuthState = "0"
		}
	}

	return mbrType, pAuthState, eAuthState, stateReason, err
}

func (m MemberRepo) GetEntiAuthState(id int, mbrType string) (string, error) {
	var sql, eAuthState string
	var list orm.ParamsList
	o := orm.NewOrm()
	if mbrType == Mbr_cargo_owner_type {
		sql = fmt.Sprintf(SQL_Mbr_Get_Enti_Auth_State, "m_cargo_owner")
	} else if mbrType == Mbr_carrier_owner_type {
		sql = fmt.Sprintf(SQL_Mbr_Get_Enti_Auth_State, "m_carrier_owner")
	} else {
		sql = fmt.Sprintf(SQL_Mbr_Get_Enti_Auth_State, "m_logistics_emp")
	}
	num, err := o.Raw(sql, id).ValuesFlat(&list)
	if num >= 1 && err == nil {
		eAuthState = list[0].(string)
	} else {
		eAuthState = "0"
	}
	return eAuthState, err
}

func (r MemberRepo) GetPersonalAuthInfo(id int) (string, string, error) {
	var idCardFs, idCardRs string
	var list orm.ParamsList
	o := orm.NewOrm()
	num, err := o.Raw(SQL_Mbr_Get_Personal_Auth_Info, id).ValuesFlat(&list)
	if num >= 1 && err == nil {
		if list[0] != nil {
			idCardFs = list[0].(string)
		}
		if list[1] != nil {
			idCardRs = list[1].(string)
		}
	}
	return idCardFs, idCardRs, err
}

func (r MemberRepo) AddRole(id int, mbrType string) error {
	var sql string
	var err error
	//var eAuthState string
	o := orm.NewOrm()
	mbr := &MemberDto{Id: id}
	mbr.TypeId, _ = strconv.Atoi(mbrType)
	if _, err = o.Raw(SQL_Mbr_Update_Member_Type, mbr.TypeId, id).Exec(); err == nil {
		if r.checkRoleNotExists(id, mbrType) {
			if mbrType == Mbr_carrier_owner_type || mbrType == Mbr_ship_owner_type {
				sql = fmt.Sprintf(SQL_Mbr_Add_Role, "m_carrier_owner")
			} else if mbrType == Mbr_cargo_owner_type {
				sql = fmt.Sprintf(SQL_Mbr_Add_Role, "m_cargo_owner")
			} else {
				sql = fmt.Sprintf(SQL_Mbr_Add_Role, "m_logistics_emp")
			}
			_, err = o.Raw(sql, id).Exec()
		}
	}
	return err
}

func (r MemberRepo) checkRoleNotExists(id int, mbrType string) bool {
	var sql string
	var list orm.ParamsList
	o := orm.NewOrm()
	if mbrType == Mbr_carrier_owner_type || mbrType == Mbr_ship_owner_type {
		sql = fmt.Sprintf(SQL_Mbr_Role_Exists, "m_carrier_owner")
	} else if mbrType == Mbr_cargo_owner_type {
		sql = fmt.Sprintf(SQL_Mbr_Role_Exists, "m_cargo_owner")
	} else {
		sql = fmt.Sprintf(SQL_Mbr_Role_Exists, "m_logistics_emp")
	}
	num, err := o.Raw(sql, id).ValuesFlat(&list)
	return num == 0 && err == nil
}
